﻿using blackhouse.mojoPortalExtend;
using blackhouse.NemoBlog.Business;
using blackhouse.NemoBlog.Business.Presenter;
using blackhouse.NemoBlog.Business.View;
using blackhouse.WebNemo.Business.Groups;
using mojoPortal.Business;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.NemoBlog.Web {
    public partial class StandardNemoBlogLastPostsModule : UserControl, IPostListView {

        #region Fields

        private int lastPostId = -1;

        #endregion

        #region Properties

        public SiteModuleControl ParentControl {
            get;
            set;
        }

        public int PlaceId {
            get {
                return WebUtils.ParseInt32FromHashtable(this.ParentControl.Settings, "SelectBlog", this.ParentControl.ModuleId);
            }
        }

        public int PageId {
            get {
                return this.ParentControl.ModuleConfiguration.PageId;
            }
        }

        public bool IsPublisher {
            get;
            set;
        }

        public bool BlogSubtitleVisible {
            get {
                return this.ltlBlogSubtitle.Visible;
            }
            set {
                this.ltlBlogSubtitle.Visible = value;
            }
        }

        public string BlogTitle {
            get {
                return this.ltlBlogTitle.Text;
            }
            set {
                this.ltlBlogTitle.Text = value;
                this.blogLink.ToolTip = value;
            }
        }

        public string BlogSubtitle {
            get {
                return this.ltlBlogSubtitle.Text;
            }
            set {
                this.ltlBlogSubtitle.Text = value;
            }
        }

        public bool PostsVisible {
            get {
                return this.rptPosts.Visible;
            }
            set {
                this.rptPosts.Visible = true;
            }
        }

        public bool NoPostsVisible {
            get {
                return !this.PostsVisible;
            }
            set {
                this.PostsVisible = !value;
            }
        }

        public int SelectedTagId {
            get { return 0; }
        }

        public int LastPostId {
            get {
                if (this.lastPostId == -1) {
                    string sV = Request["lastId"];
                    int v = 0;
                    if (!String.IsNullOrEmpty(sV)
                        && Int32.TryParse(sV, out v))
                        this.lastPostId = v;
                    else
                        this.lastPostId = 0;
                }
                return this.lastPostId;
            }
        }

        public bool SeeMoreButtonVisible {
            get {
                return this.seeMoreLink.Visible;
            }
            set {
                this.seeMoreLink.Visible = value;
            }
        }

        public bool RequireSnapshoot {
            get {
                return Request.QueryString.AllKeys.Contains("_escaped_fragment_");
            }
        }

        #endregion

        #region Page Cycle

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
        
            PageModule pm= PageModule.GetPageModulesByModule(this.PlaceId).FirstOrDefault();
            if (pm != null && pm.PageId != this.ParentControl.PageId) {
                this.blogLink.NavigateUrl = PageProvider.AdjustUrlToCurrentSite(pm.PageUrl);
            }
            else
                this.blogLink.Visible = false;

            this.seeMoreLink.Attributes.Add("data-action", "load-more-posts");
            mojoBasePage cmsPage = this.Page as mojoBasePage;
            if (cmsPage != null)
                cmsPage.AdditionalMetaMarkup = "<meta name=\"fragment\" content=\"!\">";

        }

        #endregion

        #region Methods

        public string SuggestDefaultTitle() {
            return this.ParentControl.Title;
        }

        public void SetSettingsLink(IGroup settingsGroup, int recordId) {
        }

        public void SetAddNewLink(IGroup postsGroup, int pageId, int moduleId) {
        }

        public void SetAddNewLinkInNoPostContent(IGroup postsGroup, int pageId, int moduleId) {
        }

        public void PopulatePosts(NemoBlogPost[] postsToLoad) {
            this.rptPosts.DataSource = postsToLoad.Select(p => new DTNemoBlogPost(p));
            this.rptPosts.DataBind();
        }

        public void SetSeo(string title, string url) {
            PageSeo seo = new PageSeo(title, String.Empty, Request.Url.GetLeftPart(UriPartial.Authority) + PageProvider.AdjustUrlToCurrentSite(url));
            PageProvider pp = new PageProvider();
            pp.SetSeo(seo);
        }

        #endregion

    }
}